﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Infragistics.WebUI.UltraWebGrid;

public partial class shop_Order_Manager : PageBase
{
    private static DataTable dt = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    {
        this.RegisterJS();

        if (!IsPostBack)
        {
            InitComponents();
            ReadAction();

            if (_Customer != null)
            {
                hCustomerType.Value = _Customer.CustomerTypeID.ToString();
                hCustomerID.Value = _Customer.ID.ToString();
                hSiteID.Text = _Customer.ShopID.ToString() ;
            }

            BindData();
        }
    }

    #region Web 窗体设计器生成的代码

    protected override void OnLoad(EventArgs e)
    {
        isRequestLogin = true;                                  //是否需要登录
        RequestLoginPage = this.Request.Url.AbsoluteUri;        //获取决对Url
        isAtFramePageLogin = true;                              //是框架页

        //        RequestCompetences = Competences.BuildCompetencesList(Competences.ZZCGD_48);//页面权限

        base.OnLoad(e);
    }

    #endregion

    protected void btnCheck_Click(object sender, EventArgs e)
    {
        long SalesID = Shove._Convert.StrToLong(hID.Value, 0);                     //订单ID
        long VerfyOperID = _Customer.ID;                                          //操作会员ID

        long ID = DAL.Procedures.P_Site_ShopOrderCheck(SalesID, VerfyOperID);
        if (ID >= 0)
        {
            Shove._Web.JavaScript.Alert(this.Page, "审核订单成功！");
        }

        this.hID.Value = "";
        this.BindData();
    }

    protected void btnDel_Click(object sender, EventArgs e)
    {
        long SalesID = Shove._Convert.StrToLong(hID.Value, 0);                     //订单ID
        long VerfyOperID = _Customer.ID;                                          //操作会员ID

        long ID = DAL.Procedures.P_Site_ShopOrderCancel(SalesID, VerfyOperID);
        if (ID >= 0)
        {
            Shove._Web.JavaScript.Alert(this.Page, "取消订单成功！");
        }

        this.hID.Value = "";
        this.BindData();
    }

    protected void btnOut_Click(object sender, EventArgs e)
    {
        long SalesID = Shove._Convert.StrToLong(hID.Value, 0);                     //订单ID
        string Return = "";

        long ID = DAL.Procedures.P_Site_ShopOrderOut(_Site.ID, _Customer.ID, SalesID, "商城订单出库", ref Return);

        if (Return != "")
        {
            Shove._Web.JavaScript.Alert(this.Page, Return);

            return;
        }

        if (ID >= 0)
        {
            Shove._Web.JavaScript.Alert(this.Page, "订单已出库"); //，生成发货单，请完善发货单确认发货！");
        }
        else
        {
            Shove._Web.JavaScript.Alert(this.Page, "订单未能成功出库！");
        }

        this.hID.Value = "";
        this.BindData();
    }

    protected void btnExcel_Click(object sender, EventArgs e)
    {
        if (dt != null && dt.Rows.Count > 0)
        {
            dt.Columns[0].ColumnName = "序号";
            dt.Columns[1].ColumnName = "订单编号";
            dt.Columns[2].ColumnName = "销售商城";
            dt.Columns[3].ColumnName = "会员编号";
            dt.Columns[4].ColumnName = "会员名称";
            dt.Columns[5].ColumnName = "总金额";
            dt.Columns[6].ColumnName = "制单日期";
            dt.Columns[7].ColumnName = "状态";
            dt.Columns[8].ColumnName = "收货人";
            dt.Columns[9].ColumnName = "收货人电话";
            dt.Columns[10].ColumnName = "收货人手机";
            dt.Columns[11].ColumnName = "收货地址";
            dt.Columns[12].ColumnName = "发票";

            Utility.DataTableToExcel.DataTableToExcelAndDownload(dt, "Order_Manager.xls");
        }
        else
        {
            PublicFunction.GoError(ErrorNumber.NoData, "没有数据，不能导出到Excel!", "Order_Manager");
        }
    }

    protected void btPrint_Click(object sender, EventArgs e)
    {
        Response.Write("<script language='javascript'>window.open('Order_Print.aspx?ID=" + hID.Value + "')</script>");

        DAL.Tables.tbl_Shop_PrintNum table = new DAL.Tables.tbl_Shop_PrintNum();

        long orderId = Shove._Convert.StrToLong((hID.Value), 0);                            //订单ID
        int num = Shove._Convert.StrToInt((hNum.Value), 0);                                 //最新次数
        string condition = "[Type]=1 and [OrderID]=" + orderId;

        if (num != 0)
        {
            table.Num.Value = num;
            table.Update(condition);     //更新打印次数
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string sCondition = "SalesType = 4 and CreateShopID=" + _Site.ID.ToString();

        //单据号
        if (txtData.Text.Trim() != "")
        {
            sCondition += " and SalesNo='" + txtData.Text.Trim() + "'";
        }

        //时间
        if (txtBeginDate.Text != "" && txtEndDate.Text != "")
        {
            DateTime dtStart = Shove._Convert.StrToDateTime(txtBeginDate.Text, DateTime.Now.ToString("yyyy-MM-dd"));
            DateTime dtEnd = Shove._Convert.StrToDateTime(txtEndDate.Text, DateTime.Now.ToString("yyyy-MM-dd"));
            TimeSpan ts = dtStart - dtEnd;

            if (ts.Days > 0 || ts.Hours > 0 || ts.Minutes > 0)
            {
                Shove._Web.JavaScript.Alert(this.Page, "开始时间晚于结束时间！");

                return;
            }
            else
            {
                sCondition += " and " + ddlDateType.SelectedValue + " between '" + dtStart.ToString("yyyy-MM-dd") + "' and '" + dtEnd.AddDays(1).ToString("yyyy-MM-dd") + "'";
            }
        }

        dt = new DAL.Views.V_Shop_SalesMaster().Open("", sCondition, "");

        uwgMain.DataSource = dt;
        uwgMain.DataBind();
    }

    protected void uwgMain_PageIndexChanged(object sender, Infragistics.WebUI.UltraWebGrid.PageEventArgs e)
    {
        this.BindData();
    }

    protected void uwgMain_ActiveRowChange(object sender, RowEventArgs e)
    {
        string sID = this.uwgMain.Rows[e.Row.Index].Cells[0].Value.ToString();
        this.BindDataDetail(sID);
        trDetail.Visible = true;
    }

    private void RegisterJS()
    {
        // AjaxPro.Utility.RegisterTypeForAjax(typeof(Ajax.PrintNumCheck));

        Infragistics.WebUI.Shared.Util.ClientScript.ResolveAndRegisterClientScriptInclude(
             Shove._Web.Utility.GetUrl() + "/js/Scripts/ig_webgrid_an.js",
             "",
             Shove._Web.Utility.GetUrl() + "/js/Scripts/ig_webgrid_an.js",
             "ig_webgrid_an.js", this.uwgMain, "ig_webgrid_an");          //注册 Infragistics js和控件        
    }

    private void ReadAction()
    {
        string sType = Shove._Web.Utility.GetRequest("Type");

        if (sType == "Check")
        {
            btnCheck.Visible = true;
            btnDel.Visible = true;
            btnOut.Visible = false;
            btnExcel.Visible = false;
            btnPrint.Visible = false;

            WebPanel1.Header.Text = "确认付款";
        }
        else if (sType == "Query")
        {
            btnCheck.Visible = false;
            btnDel.Visible = false;
            btnOut.Visible = false;
            btnExcel.Visible = true;
            btnPrint.Visible = false;

            WebPanel1.Header.Text = "订单查询";
        }
        else if (sType == "Out")
        {
            btnCheck.Visible = false;
            btnDel.Visible = false;
            btnOut.Visible = true;
            btnExcel.Visible = false;
            btnPrint.Visible = true;

            WebPanel1.Header.Text = "订单出库";
        }
    }

    private void BindData()
    {
        if (dt != null && dt.Rows.Count > 0)
        {
            dt.Clear();
        }
        long ShopID = Shove._Convert.StrToLong(hSiteID.Text, 0);
        string sType = Shove._Web.Utility.GetRequest("Type");
        string sCondtion = "SalesType = 4 and ShopID=" + ShopID;     
        long CustomerID = Shove._Convert.StrToLong(hCustomerID.Value, 0);

        if (sType == "Check")
        {
            sCondtion += " and Status = 0";
        }
        else if (sType == "Query")
        {

        }
        else if (sType == "Out")
        {
            sCondtion += " and Status =1";
        }

        string FieldList = "ID,SalesNO,CreateShopNo,CustomerNo,CustomerName,ActualMoney,CreateDate,State,ConsigneeName,ConsigneeTel,ConsigneeMobile,ConsigneeAdress,Ticket";

        dt = new DAL.Views.V_Shop_SalesMaster().Open(FieldList, sCondtion, "");

        uwgMain.DataSource = dt;
        uwgMain.DataBind();
    }

    private void InitComponents()
    {
        txtBeginDate.Attributes.Add("onclick", "WdatePicker()");
        txtEndDate.Attributes.Add("onclick", "WdatePicker()");

        Utility.InitUltraWebGrid.InitDisplayLayOut(this.uwgMain);
        Utility.InitUltraWebGrid.InitDisplayLayOut(this.uwgDetail);
    }

    /// <summary>
    /// 加载该订单下的所有商品信息
    /// </summary>
    private void BindDataDetail(string sID)
    {
        if (dt != null && dt.Rows.Count > 0)
        {
            dt.Clear();
        }

        dt = new DAL.Tables.tbl_Shop_SalesDetail().Open("", "SalesID=" + sID, "");

        uwgDetail.DataSource = dt;
        uwgDetail.DataBind();
    }
}
